package com.lht.leetcodeDemo;

import org.junit.Test;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-05-28
 */
public class RemoveLinkedListElements {


    @Test
    public void test(){

        int[] arr=new int[]{1,1};

        ListNode head=new ListNode(0);
        ListNode cur=head;
        for(int i=0;i<arr.length;i++){
            cur.next=new ListNode(arr[i]);
            cur=cur.next;
        }

        head=removeElements(head.next,1);
        while (head != null) {
            System.out.print(head.val+",");
            head = head.next;
        }
    }

    public ListNode removeElements(ListNode head, int val) {
        if(head==null){
            return null;
        }
        ListNode h=new ListNode(-1);
        h.next=head;
        ListNode cur= h;
        while (cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        return h.next;
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
